// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.FeedCore.Helpers;
using svs.Mobile.BeyondMedia.Objects;
using svs.Mobile.BeyondMedia.UiCore.KineticControls;

namespace svs.Mobile.BeyondMedia.Controls
{
	/// <summary>
	/// Provide a 2 line listview
	/// </summary>
	internal class KineticCategoryListView : KineticListView, IView
	{

		public enum CustomizeFor
		{
			CategorySelection,
			CategoryManagement
		}
	
		public CustomizeFor _CustomizedFor = CustomizeFor.CategorySelection;

		#region Delegates

		public delegate void CategorySelectedEvent(FeedCategory category);

		#endregion

		private readonly CategoryList m_Items = new CategoryList();

		protected override void OnCustomizeControlSettings(ListViewSettings settings)
		{
			settings.PrimaryIconWidth = 0;
			settings.ListItemBackgroundColorOdd = Color.Gainsboro;
			settings.ListItemBackgroundColorEven = Color.LightGray;
			settings.ListItemSecondaryTextColor = Color.DarkGray;
		}

		public CategoryList Items
		{
			get { return m_Items; }
		}

		public FeedCategory SelectedCategory
		{
			get
			{
				if (SelectedIndex != -1 && m_Items.Count > 0)
				{
					return m_Items[SelectedIndex];
				}
				
				return null;
			}
		}

		public event CategorySelectedEvent Select;

		public void SetCategories(CategoryList value, FeedCategory selectedCatergory)
		{
			Clear();
			Items.AddRange(value);
			SetItems(BuildNativeControlItems(), GetItemIndex(selectedCatergory), false);
		}

		protected override void OnKeyDown(KeyEventArgs e)
		{
			switch (e.KeyCode)
			{
				case Keys.Return:

					e.Handled = true;
					OnCategorySelected();
					break;

				case Keys.Left:
					OnCancel();
					e.Handled = true;
					break;

				case Keys.Right:
					OnCancel();
					e.Handled = true;
					break;
			}

			if (!e.Handled)
			{
				base.OnKeyDown(e);
			}
		}

		protected override void OnKeyUp(KeyEventArgs e)
		{
			// Ignore the Up as the selection is done on Down
		}

		protected override void OnBackKeyClicked()
		{
			OnCancel();
		}

		private void OnCancel()
		{
			SelectedIndex = -1;
			OnCategorySelected();
		}

		protected override void  OnItemBodyClicked(int index)
		{
				OnCategorySelected();
		}

		private void OnCategorySelected()
		{
			if (Select != null)
			{
				Select(SelectedCategory);
			}
		}

		/// <summary>
		/// Clears this instance.
		/// </summary>
		public void Clear()
		{
			SelectedIndex = -1;
			Items.Clear();
		}

		protected override List<ListViewItemData> BuildNativeControlItems()
		{
			var retVal = new List<ListViewItemData>(m_Items.Count);

			foreach (var category in Items)
			{
				retVal.Add(BuiltNativeControlItem(category));
			}

			return retVal;
		}

		private ListViewItemData BuiltNativeControlItem(FeedCategory category)
		{
			var i = new ListViewItemData
			        	{
			        		PrimaryText = category.Name,
			        		SecondaryText = category.NumberOfFeedsInCategory + " Feeds",
									SecondaryImage = CustomizedFor == CustomizeFor.CategorySelection ? FeedImageList.MoreArrowImage : null,
									BoldPrimary = CustomizedFor == CustomizeFor.CategorySelection && category.HasUnreadFeeds
								};

			return i;
		}

		public void Reload()
		{
			SetItems(BuildNativeControlItems(), SelectedIndex, false);
		}

		private int GetItemIndex(FeedCategory category)
		{
			int index = -1;

			if (Items.Count > 0)
			{
				if (category != null)
				{
					index = Items.IndexOf(category);
				}
			}
			return index;
		}

		public CustomizeFor CustomizedFor
		{
			get { return _CustomizedFor; }
			set { _CustomizedFor = value; }
		}

		public string Title
		{
			get
			{
				return string.Format("Categories {0}",
														 m_Items == null || m_Items.Count == 0 ? string.Empty : "(" + m_Items.Count + ")");
			}
		}

		public MainMenu ViewMenu
		{
			get
			{
				return MenuManager.GetMenu(Helpers.ViewNames.Categories);
			}
		}

		public void OnBeforeActivate()
		{
			SetCategories(CategoryManager.GetCategoriesForFilter(), Configuration.ActiveFeedCategory);
		}

		public void OnAfterDeactivate()
		{
			
		}

		public void OnAfterActivate()
		{
			
		}

	}
}